package plugins.AI;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.sql.Date;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态表单插件 - 饮食计划单据生成
 */
public class DietPlan implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String, String> result = new HashMap<>();
        if ("GET_JSON_STRING".equalsIgnoreCase(action)) {
            // 获取Json字符串
            String jsonResult = params.get("JsonResult").replaceAll("\\s*|\\r|\\n|\\t", "");

            JSONObject resultJsonObject = JSON.parseObject(jsonResult);

            // 提炼出json信息
            String pattern = resultJsonObject.getString("pattern"); // 饮食计划模式名称

            JSONObject userData = resultJsonObject.getJSONObject("userData");
            String name = userData.getString("name"); // 用户姓名
            String sex = userData.getString("sex"); // 用户性别
            String height = userData.getString("height"); // 用户身高
            String weight = userData.getString("weight"); // 用户体重
            String bmi = userData.getString("bmi"); // 用户BMI

            JSONObject planData = resultJsonObject.getJSONObject("plan");
            String plan = planData.toString(); // 饮食计划内容

            int cnt = planData.size();

            // String cntObject = resultJsonObject.getString("cnt"); // 饮食计划天数
            // int cnt = Integer.parseInt(cntObject); // 将cnt转换为整数

            // 生成饮食计划单据编号
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                sb.append((char) (48 + Math.random() * 10));
            }
            String billNo = sb.toString();

            // 创建饮食计划单据对象
            DynamicObject dietPlan = BusinessDataServiceHelper.newDynamicObject("dcb4_dietplan");

            // 设置对应属性
            dietPlan.set("billno", billNo);
            dietPlan.set("dcb4_name", name);
            dietPlan.set("dcb4_sex", sex);
            dietPlan.set("dcb4_pattern", pattern);
            dietPlan.set("dcb4_Height", height);
            dietPlan.set("dcb4_weight", weight);
            dietPlan.set("dcb4_bmi", bmi);

            // 动态填充单据体
            DynamicObjectCollection planEntries = dietPlan.getDynamicObjectCollection("dcb4_plan");
            planEntries.clear(); // 清空现有的计划条目

            for (int i = 0; i < cnt; i++) {
                DynamicObject entry = planEntries.addNew();

                // 获取每日计划内容
                String dayKey = "day" + i;
                if (planData.containsKey(dayKey)) {
                    JSONObject dayPlan = planData.getJSONObject(dayKey);
                    entry.set("dcb4_time", dayPlan.getString("time"));
                    entry.set("dcb4_breakfast", dayPlan.getString("breakfast"));
                    entry.set("dcb4_br_energy", dayPlan.getString("br_nutrition"));
                    entry.set("dcb4_lunch", dayPlan.getString("lunch"));
                    entry.set("dcb4_lu_energy", dayPlan.getString("lu_nutrition"));
                    entry.set("dcb4_dinner", dayPlan.getString("dinner"));
                    entry.set("dcb4_di_energy", dayPlan.getString("dr_nutrition"));
                }
            }


            // 保存饮食计划单据
            SaveServiceHelper.saveOperate("dcb4_dietplan", new DynamicObject[]{dietPlan}, null);
            Long pkId = (Long) dietPlan.getPkValue();

            // 拼接URL字符串
            String targetForm = "bizAction://currentPage?gaiShow=1&selectedProcessNumber=processNumber&gaiAction=showBillForm&gaiParams={\"appId\":\"dcb4_shouye\",\"billFormId\":\"dcb4_dietplan\",\"billPkId\":\"" + pkId + "\"}&title=饮食计划单据 &iconType=bill&method=bizAction";
            result.put("formUrl", targetForm);
        }
        return result;
    }
}
